package com.cuit.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.mapper.OrgMapper;
import com.cuit.mapper.RolesMapper;
import com.cuit.mapper.UsersMapper;
import com.cuit.params.*;
import com.cuit.pojo.Org;
import com.cuit.pojo.Roles;
import com.cuit.pojo.Users;
import com.cuit.service.UserService;
import com.cuit.utils.HandlePasswordUtil;
import com.cuit.utils.RedisUtils;
import com.cuit.utils.Result;
import com.cuit.utils.UserThreadLocal;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author SYE
 * @date 2023/7/27 14:25
 * @desciption:
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UsersMapper userMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private OrgMapper orgMapper;

    @Override
    public Result resetPwd(SetPasswordParams setPassword, String token) {
        Users users = UserThreadLocal.get();
        boolean isWeakPassword = HandlePasswordUtil.isWeekPassword(setPassword.getNewPwd());
        if (isWeakPassword){
            return Result.fail(201,"密码强度低请重新输入");
        }
        if (users.getPassword().equals(DigestUtils.md5DigestAsHex(setPassword.getOldPwd().getBytes()))){
            LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(Users::getLoginName,users.getLoginName());
            updateWrapper.eq(Users::getPassword,DigestUtils.md5DigestAsHex(setPassword.getOldPwd().getBytes()));
            updateWrapper.set(Users::getPassword,DigestUtils.md5DigestAsHex(setPassword.getNewPwd().getBytes()));
            int flag = userMapper.update(null,updateWrapper);
            if (flag != 1){
                return Result.fail(201,"更新失败");
            }
            redisUtils.del(token);
        }else {
            return Result.fail(20,"两次输入的密码不一致");
        }
        return Result.success(null);
    }

    @Override
    public Result addUser(UserParams userParams) {
        LambdaUpdateWrapper<Users> queryLoginName = new LambdaUpdateWrapper<>();
        queryLoginName.eq(Users::getLoginName,userParams.getLoginName());
        Users users1 = userMapper.selectOne(queryLoginName);
        if (users1!=null){
            return Result.fail(201,"账号重复请重新输入");
        }
        Users users = new Users();

        users.setLoginName(userParams.getLoginName());
        users.setPassword(DigestUtils.md5DigestAsHex(userParams.getPassword().getBytes()));
        users.setUsersName(userParams.getUsersName());
        users.setRoleId(userParams.getRoleId());
        users.setOrgId(userParams.getOrgId());
        if (userMapper.insert(users)==0){
            return Result.fail(201,"添加数据失败");
        }
        return Result.success(1);
    }

    @Override
    public Result resetEZPwd(SetPasswordParams setPasswordParams) {
        int flag = 0;
        boolean isWeakPassword = HandlePasswordUtil.isWeekPassword(setPasswordParams.getNewPwd());
        if (isWeakPassword){
            return Result.fail(201,"密码强度低请重新输入");
        }
        LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(Users::getPassword,DigestUtils.md5DigestAsHex(setPasswordParams.getNewPwd().getBytes()));
        updateWrapper.eq(Users::getLoginName,setPasswordParams.getLoginName());
        updateWrapper.eq(Users::getPassword,DigestUtils.md5DigestAsHex(setPasswordParams.getOldPwd().getBytes()));
        flag = userMapper.update(null, updateWrapper);
        if (flag == 0){
            return Result.fail(201,"更新密码失败");
        }
        return Result.success(flag);
    }

    /**
     * 删除指定id的用户
     * @param userId
     * @return
     */
    @Override
    public Result deleteUser(String userId) {
        LambdaQueryWrapper<Users> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getUserId,userId);
        if (userMapper.delete(queryWrapper)!=1){
            return Result.fail(201,"删除用户失败");
        }
        return Result.success(1);
    }

    /**
     * 获得全部用户信息
     * @return
     */
    @Override
    public Result getAllUser(Integer page, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("page",(page-1)*pageSize);
        map.put("pageSize",pageSize);
        List<UserShowParams> allUser = userMapper.getAllUser(map);
        Long total = userMapper.getAllUserCount();
        if (allUser.isEmpty()){
            return Result.fail(201,"用户数据为空");
        }
        Map<String,Object> allUserMap = new HashMap<>();
        allUserMap.put("record",allUser);
        allUserMap.put("total",total);
        return Result.success(allUserMap);
    }

    /**
     * 根据条件查询用户信息
     * @param userOption
     * @return
     */
    @Override
    public Result queryByOption(UserOption userOption,Integer page, Integer pageSize) {
        Map<String,Object> map = new HashMap<>();
        map.put("page",(page-1)*pageSize);
        map.put("pageSize",pageSize);
        map.put("UserOption",userOption);
        List<UserParams> userParamsList = userMapper.queryByOption(map);
        Long total = userMapper.queryOptionCount(userOption);
        if (userParamsList.isEmpty()){
            return Result.fail(410,"查询数据为空");
        }
        Map<String,Object> allUserMap = new HashMap<>();
        allUserMap.put("record",userParamsList);
        allUserMap.put("total",total);
        return Result.success(allUserMap);
    }

    /**
     * 重置用户的密码
     * @param setPasswordParams
     * @return
     */
    @Override
    public Result resetUserPwd(SetPasswordParams setPasswordParams) {
        LambdaUpdateWrapper<Users> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Users::getLoginName,setPasswordParams.getLoginName());
        updateWrapper.set(Users::getPassword,DigestUtils.md5DigestAsHex(setPasswordParams.getNewPwd().getBytes()));
        int flag = userMapper.update(null,updateWrapper);
        if (flag!=1){
            return Result.fail(201,"重置密码失败");
        }
        return Result.success(flag);
    }
}
